<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use Think\Controller;

class PersonalController extends AdminController {

    /**
     * 用户管理首页
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    public function index(){
		/*
        $nickname       =   I('name');
        $map['status']  =   array('egt',0);
        if(is_numeric($nickname)){
            $map['uid|nickname']=   array(intval($nickname),array('like','%'.$nickname.'%'),'_multi'=>true);
        }else{
            $map['nickname']    =   array('like', '%'.(string)$nickname.'%');
        }
		*/
		$id = session("userID");
		//dump($id );
		if(!IS_POST){
			$user = M('User')->where(array('id'=>$id))->find();
			//dump($user );
			//dump($list);
			$this->assign('user', $user);
			$this->meta_title = '用户信息';
			$this->display();
		}else{
			/* 检测密码 */
            if(!empty($password) && $password != $rpassword){
                $this->error('密码存在必须一致！');
            }elseif(!empty($password)){
				$data['password'] = $password;
			}
			
			$data['email'] = I('email');
			$data['username'] = I('name');
			$data['status'] = I('status');
			$data['nickname'] = I('nickname');
			
			$user = M('User')->where(array('id'=>$id))->save($data);
			$this->success('资料修改成功！');
		}
    }
	
	public function password(){
		$id = session("userID");
		//dump($id );
		if(!IS_POST){

			$this->meta_title = '用户信息';
			$this->display();
		}else{
			/* 检测密码 */
            if(!empty($password) && $password != $rpassword){
                $this->error('密码存在必须一致！');
            }elseif(!empty($password)){
				$data['password'] = $password;
			}else{
				$this->error('密码为空！');
			}
			
			
			$user = M('User')->where(array('id'=>$id))->save($data);
			$this->success('密码修改成功！');
		}
	}

    /**
     * 修改昵称初始化
     * @author huajie <banhuajie@163.com>
     */
    public function updateNickname(){
        $nickname = M('Member')->getFieldByUid(UID, 'nickname');
        $this->assign('nickname', $nickname);
        $this->meta_title = '修改昵称';
        $this->display();
    }

    /**
     * 修改密码初始化
     * @author huajie <banhuajie@163.com>
     */
    public function edit($id = 0,$name = '', $password = '', $rpassword = '', $email = ''){
	
		if(empty($id)){
			$this->error('请选择用户ID');
		}
		
        if(IS_POST){
            /* 检测密码 */
            if(!empty($password) && $password != $rpassword){
                $this->error('密码存在必须一致！');
            }

            
                $user = array('email' => $email, 'password' =>md5($password), 'nickname' => $username, 'status' => 1 ,
					'reg_time' => time(),'reg_ip'=>ip2long(get_client_ip()),
				);
                if(!M('User')->add($user)){
                    $this->error('用户添加失败！');
                } else {
                    $this->success('用户添加成功！',U('Admin/Index/index'));
                }
        } else {
			$User = M("User"); 
			$data = $User->where(array('id'=>$id,'status' => 1))->find();
			//dump($data);
            //$this->meta_title = '新增用户';
			
			$this->assign('data', $data);
            $this->display();
        }
    }

    /**
     * 修改密码提交
     * @author huajie <banhuajie@163.com>
     */
    public function submitPassword(){
        //获取参数
        $password   =   I('post.old');
        empty($password) && $this->error('请输入原密码');
        $data['password'] = I('post.password');
        empty($data['password']) && $this->error('请输入新密码');
        $repassword = I('post.repassword');
        empty($repassword) && $this->error('请输入确认密码');

        if($data['password'] !== $repassword){
            $this->error('您输入的新密码与确认密码不一致');
        }

        $Api    =   new UserApi();
        $res    =   $Api->updateInfo(UID, $password, $data);
        if($res['status']){
            $this->success('修改密码成功！');
        }else{
            $this->error($res['info']);
        }
    }

    /**
     * 用户行为列表
     * @author huajie <banhuajie@163.com>
     */
    public function action(){
        //获取列表数据
        $Action =   M('Action')->where(array('status'=>array('gt',-1)));
        $list   =   $this->lists($Action);
        int_to_string($list);
        // 记录当前列表页的cookie
        Cookie('__forward__',$_SERVER['REQUEST_URI']);

        $this->assign('_list', $list);
        $this->meta_title = '用户行为';
        $this->display();
    }

    /**
     * 新增行为
     * @author huajie <banhuajie@163.com>
     */
    public function addAction(){
        $this->meta_title = '新增行为';
        $this->assign('data',null);
        $this->display('editaction');
    }

    /**
     * 编辑行为
     * @author huajie <banhuajie@163.com>
     */
    public function editAction(){
        $id = I('get.id');
        empty($id) && $this->error('参数不能为空！');
        $data = M('Action')->field(true)->find($id);

        $this->assign('data',$data);
        $this->meta_title = '编辑行为';
        $this->display();
    }

    /**
     * 更新行为
     * @author huajie <banhuajie@163.com>
     */
    public function saveAction(){
        $res = D('Action')->update();
        if(!$res){
            $this->error(D('Action')->getError());
        }else{
            $this->success($res['id']?'更新成功！':'新增成功！', Cookie('__forward__'));
        }
    }

    /**
     * 会员状态修改
     * @author 朱亚杰 <zhuyajie@topthink.net>
     */
    public function changeStatus($method=null){
        $id = array_unique((array)I('id',0));
        if( in_array(C('USER_ADMINISTRATOR'), $id)){
            $this->error("不允许对超级管理员执行该操作!");
        }
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['uid'] =   array('in',$id);
        switch ( strtolower($method) ){
            case 'forbiduser':
                $this->forbid('Member', $map );
                break;
            case 'resumeuser':
                $this->resume('Member', $map );
                break;
            case 'deleteuser':
                $this->delete('Member', $map );
                break;
            default:
                $this->error('参数非法');
        }
    }

}
